/**
 *
 */
package com.levy.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;


import org.apache.commons.lang.StringUtils;


/**
 * 
 * @author levy.xia
 *
 */
@Entity(name = "dm_resource")
public class Resource implements Serializable {

    /**
     * the serialVersionUID.
     */
    private static final long serialVersionUID = -1792606827665456308L;

    /**
     * the resource id.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sq_resource")
    @SequenceGenerator(name = "sq_resource", sequenceName = "dm_sq_user_role")
    private Integer id;

    /**
     * the resource type.
     */
    private String type;

    /**
     * the resource value.
     */
    private String value;

    /**
     * the roles of this resource.
     */
    @ManyToMany(mappedBy = "resources", targetEntity = Role.class, fetch = FetchType.EAGER)
    private Set<Role> roles;

    /**
     * The default constructor.
     */
    public Resource() {

    }

    /**
     *
     *
     * @return Get role authorities as string.
     */
    //@Transient
    public final String getRoleAuthorities() {
        List<String> roleAuthorities = new ArrayList<String>();
        //if(null==roles)
        //{
        //   roleAuthorities.add("ROLE_ANONYMOUS");
        //}
        //else
        //{
        for (Role role : roles) {
            roleAuthorities.add(role.getName());
        }
        //}
        return StringUtils.join(roleAuthorities.iterator(), ",");
    }

    /**
     * @return the id
     */
    public final Integer getId() {
        return id;
    }

    /**
     * @return the type
     */
    public final String getType() {
        return type;
    }

    /**
     * @return the value
     */
    public final String getValue() {
        return value;
    }

    /**
     * @return the roles
     */
    public final Set<Role> getRoles() {
        return roles;
    }

    /**
     * @param id the id to set
     */
    public final void setId(final Integer id) {
        this.id = id;
    }

    /**
     * @param type the type to set
     */
    public final void setType(final String type) {
        this.type = type;
    }

    /**
     * @param value the value to set
     */
    public final void setValue(final String value) {
        this.value = value;
    }

    /**
     * @param roles the roles to set
     */
    public final void setRoles(final Set<Role> roles) {
        this.roles = roles;
    }

}
